package cn.edu.nwpu.warehousemanagement.controller;

import cn.edu.nwpu.warehousemanagement.pojo.Freight;
import cn.edu.nwpu.warehousemanagement.pojo.Result;
import cn.edu.nwpu.warehousemanagement.pojo.dto.*;
import cn.edu.nwpu.warehousemanagement.service.*;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ljl、lsr
 * 货品入库控制类-负责处理入库请求
 */
@RestController
@RequestMapping("/api/freight")
public class FreightController {

    @Resource
    private InBoundService inBoundService;
    @Resource
    private OutboundService outboundService;
    @Resource
    private ProductService productService;
    @Resource
    private ShelfService shelfService;
    @Resource
    private FreightService freightService;

    /**
     * 处理入库请求
     * @param inBoundDTOs 入库数据传输对象
     * @return 返回是否成功的结果
     */
    @PostMapping("/inbound")
    public Result<?> InBound(@Valid @RequestBody List<InBoundDTO> inBoundDTOs) {
        return inBoundService.InBound(inBoundDTOs);
    }


    /**
     * 处理出库请求
     * @param outboundDTO 出库数据传输对象，包含出库相关信息
     * @return 操作结果
     */
    @PostMapping("/outbound")
    public Result<?> outbound(@RequestBody OutboundDTO outboundDTO) {
        return outboundService.outbound(outboundDTO);
    }

//    @PostMapping("/update-freight")
//    public Result<?> UpdateFreight(@RequestBody Freight freight) {
//        return null;
//    }

    //region 不分页获取请求

    // 获取在库货物（不分页）
    @PostMapping("/get-in-stock/list")
    public Result<List<Freight>> getInStockFreights() {
        return freightService.getInStockFreights();
    }

    // 获取出库货物（不分页）
    @PostMapping("/get-outbound/list")
    public Result<List<Freight>> getOutboundFreights() {
        return freightService.getOutboundFreights();
    }

    // 根据ID查询货物
    @PostMapping("/get-freight-by-id")
    public Result<Freight> getFreightById(@RequestBody IDDTO idDTO) {
        return freightService.getFreightById(idDTO);
    }

    // 根据名称查询货物（不分页）
    @PostMapping("/get-freights-by-name/list")
    public Result<?> getFreightsByName(@RequestBody NameDTO nameDTO) {
        return freightService.getFreightsByName(nameDTO, null);
    }

    // 根据货架ID获取在库货物（不分页）
    @PostMapping("/get-in-stock-by-shelf/list")
    public Result<List<Freight>> getInStockByShelf(
            @RequestBody IDDTO idDTO
    ) {
        return freightService.getInStockFreightsByShelfId(idDTO.getId());
    }

    //endregion

    //region 分页获取请求

    // 获取所有货物（分页）
    @PostMapping("/get-all-freights")
    public Result<Page<Freight>> getAllFreights(@RequestBody PageDTO pageDTO) {
        return freightService.getAllFreights(pageDTO);
    }

    // 获取在库货物（分页）
    @PostMapping("/get-in-stock/page")
    public Result<Page<Freight>> getInStockFreightsWithPage(@RequestBody PageDTO pageDTO) {
        return freightService.getInStockFreights(pageDTO);
    }

    // 获取出库货物（分页）
    @PostMapping("/get-outbound/page")
    public Result<Page<Freight>> getOutboundFreightsWithPage(@RequestBody PageDTO pageDTO) {
        return freightService.getOutboundFreights(pageDTO);
    }

    // 根据名称查询货物（分页）
    @PostMapping("/get-freights-by-name/page")
    public Result<?> getFreightsByNameWithPage(@RequestBody NameWithPageDTO request) {
        return freightService.getFreightsByName(request.getNameDTO(), request.getPageDTO());
    }

    // 根据货架ID获取在库货物（分页）
    @PostMapping("/get-in-stock-by-shelf/page")
    public Result<Page<Freight>> getInStockByShelfWithPage(
            @RequestBody ShelfIdWithPageDTO request
    ) {
        return freightService.getInStockFreightsByShelfIdWithPage(
                request.getShelfIdDTO().getId(),
                request.getPageDTO()
        );
    }

    @GetMapping("/getInStockFreightsByProductName")
    public Result<Page<Freight>> getInStockFreightsByProductName(
            @RequestParam String productName,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize){
        return freightService.searchInStockFreightsByProductName(productName, page, pageSize);
    }

    @GetMapping("/getOutStockFreightsByProductName")
    public Result<Page<Freight>> getOutStockFreightsByProductName(
            @RequestParam String productName,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize){
        return freightService.searchInStockFreightsByProductName(productName, page, pageSize);
    }

    @GetMapping("/getInStockFreightsByShelfIdAndProductName")
    public Result<Page<Freight>> getInStockFreightsByShelfIdAndProductName(
            @RequestParam Long shelfId,
            @RequestParam String productName,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize
    ){
        return freightService.getInStockFreightsByShelfAndProductName(shelfId, productName, page, pageSize);
    }

    @GetMapping("/getOutStockFreightsByShelfIdAndProductName")
    public Result<Page<Freight>> getOutStockFreightsByShelfIdAndProductName(
            @RequestParam Long shelfId,
            @RequestParam String productName,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize
    ){
        return freightService.getOutStockFreightsByShelfAndProductName(shelfId, productName, page, pageSize);
    }

    //endregion
}
